const MYSQL = require("../../sql/mysql");
// MYSQL数据库配置信息
const MYSQLConfig = require("../../sql/database");
let sqlName = MYSQLConfig.datasheetName.roles;
const existRole = require("./exist");
// 角色菜单表
const roleMenuSqlName = MYSQLConfig.datasheetName.role_menus;
/**
 * 删除菜单权限（单个）
 * @param {object} params
 * @param {string} params.id 角色id
 * @param {string} params.menu 菜单id
 * @returns {Promise<{code:string,msg:string}>}
 */
module.exports = async (params = {}) => {
  let { id, menu } = params;
  try {
    if (!id) {
      throw { code: 500, msg: "角色id不能为空" };
    } else if (!menu) {
      throw { code: 500, msg: "menu不能为空" };
    }
    await existRole({ id });
    let query = `DELETE FROM ${roleMenuSqlName} WHERE role = ${id} and menu_id = ${menu}`;
    await MYSQL.promise().query(query);
    return {
      code: 0,
      msg: "删除成功",
    };
  } catch (error) {
    throw error;
  }
};
